import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'mobx-react';
import { HashRouter as Router, Route, Redirect, Switch} from 'react-router-dom'
import { asyncComponent } from 'react-async-component';
import { Tools, Bubble } from 'weaver-mobile';
import { DocumentTitle } from "weaver-mobile-page";

import allStore from './stores';
import './style';

const getLocaleLabel = Tools.getLocaleLabel.bind(this, 'cowork_mobile');
const getLabel = Tools.getLabel;

const coms = {
  Main: asyncComponent({ // 主页
    resolve: () => import('./components/main')
  }),
  docCenter: asyncComponent({ // 公文中心
    resolve: () => import('./components/docCenter')
  }),
  docLibrary: asyncComponent({ // 公文库
    resolve: () => import('./components/docLibrary')
  }),
  docTodo: asyncComponent({ // 代办公文
    resolve: () => import('./components/docTodo')
  }),
  docLedger: asyncComponent({ // 公文台账
    resolve: () => import('./components/docLedger')
  }),
  docReport: asyncComponent({ // 公文报表
    resolve: () => import('./components/docReport')
  }),
  receiveRgist: asyncComponent({ // 收文登记
    resolve: () => import('./components/receiveRegist')
  }),
  noDocument: asyncComponent({ // 尚无正文
    resolve: () => import('./components/common/NoDocumentComponent')
  }),
  odocTypeChangeCom: asyncComponent({ // 文种变更
    resolve: () => import('./components/odocTypeChangeDialog/index')
  }),
  signApprovalHistory:asyncComponent({
      resolve:()=>import('./components/odocSignApproval/DocumentHistoryVersion')
  }),
  docExchange:asyncComponent({
      resolve:()=>import('./components/docExchange')
  })
}

class Root extends React.Component {
	
	getLocaleLabel = Tools.getLocaleLabel;

	constructor(props) {
    super(props);
    this.state = {
      loadLang: false
    }
    this.getLocaleLabel.bind(this);
	}
	
  componentDidMount() {
    Tools.ssoInit().then(() => {
      this.getLocaleLabel('cowork_mobile', () => {
			this.setState({loadLang: true});
      });
    }).catch((res) => {
      Bubble.error(getLabel(386481, '登录失败') + "," + JSON.stringify(res), 0);
    });
	}
	
  render() {


		return (
      <Provider {...allStore}>  
        <Router> 
          <Route name='home' path='/' onEnter={getLocaleLabel} render={() => {
							return (
                <coms.Main>
                  <Switch>
                    <Route name='receiveRgist' breadcrumbName='收文登记' path='/receiveRegist' component={coms.receiveRgist} />

                    <Route name='docCenter' breadcrumbName='公文中心' path='/docCenter' component={coms.docCenter} />
                    <Route name='docLibrary' breadcrumbName='公文库' path='/docLibrary' component={coms.docLibrary} />
                    <Route name='docTodo' breadcrumbName='代办公文' path='/docToDo' component={coms.docTodo}  /> 
                    <Route name='docLedger' breadcrumbName='公文台账' path='/docLedger' component={coms.docLedger}/>                  
                    <Route name='docReport' breadcrumbName='公文报表' path='/docReport' component={coms.docReport} />     
                    <Route name='noDocument' breadcrumbName='尚无正文' path='/odoc/noDocument' component={coms.noDocument} /> 
                    <Route name='odocTypeChangeCom' breadcrumbName='文种变更' path='/odoc/odocTypeChangeCom' component={coms.odocTypeChangeCom} /> 
                    <Route name='odocSignApprovalHistory' breadcrumbName='好签' path='/odoc/signApprovalHistory' component={coms.signApprovalHistory} />
                    <Route name='docExchange' breadcrumbName='公文交换' path='/docExchange' component={coms.docExchange} />
                    <Redirect exact path="/" to={{pathname: '/docCenter'}} />
                  </Switch>
                </coms.Main>
              )
        }}/>
        </Router>
      </Provider>
    );
  }
}

ReactDOM.render(<Root />, document.getElementById('root'));